
import requests
import unittest
import json
from parameterized import parameterized
from api.LoginApi import LoginApi
from tools.dbutil import DBUtil

def build_data():
    sql = "select * from t_login"
    db_data = DBUtil.exe_sql(sql)
    test_data = []
    print("db_data:",db_data)

    for case_data in db_data:
        username = case_data[2]
        password = case_data[3]
        verify_code = case_data[4]
        content_type = case_data[6]
        status_code = case_data[5]
        status = case_data[7]
        msg = case_data[8]
        test_data.append((username,password,verify_code,content_type,status_code,status,msg))
    return test_data
class TestLoginApi(unittest.TestCase):

    def setUp(self):
        self.login_api = LoginApi()
        self.session = requests.session()

    def tearDown(self):
        if self.session:
            self.session.close()

    @parameterized.expand(build_data)
    def test01_login(self,username,password,verify_code,content_type,status_code,status,msg):
        response = self.login_api.get_verify_code(self.session)
        self.assertEqual(status_code,response.status_code)
        self.assertIn(content_type,response.headers.get( "Content-Type"))

        response = self.login_api.login(self.session,username,password,verify_code)
        print("test01_login的login接口返回的数据：",response.json())
        self.assertEqual(status_code, response.status_code)
        self.assertEqual(status, response.json().get("status"))
        self.assertIn(msg, response.json().get("msg"))
